#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi: ts=4 expandtab
#
# Copyright (C) 2012 KT
#
# Author: Choonho Son <choonho.son@kt.com>
#

import subprocess
from cloudcfg.lib.cmd import Cmd

__version__="1.0"

class cmd_nis(Cmd):
	def __init__(self, logger):
		Cmd.__init__(self,logger)

	def updateHostOrder(self, params):
		"""
		update /etc/host.conf
		@params (dictionary)
		order : 

		"""
		p = params
		msg = "order %s\nmulti on" % p['order']
		fp = open('/etc/host.conf', 'w')
		fp.write(msg)
		fp.close()

	def updateNISDOMAIN(self, params):
		"""
		update /etc/sysconfig/network
		@param (dictionary)
		NISDOMAIN :
		"""
		p = params
		msg = "NISDOMAIN=%s" % p['NISDOMAIN']
		fp = open('/etc/sysconfig/network', 'a+')
		fp.write("\n%s\n" % msg)
		fp.close()
		

		
	def updateYPCONF(self,params):
		"""
		update /etc/yp.conf
		"""
		p = params
		msg = "ypserver %s.%s\n" % (p['ypserver'],p['NISDOMAIN'])
		msg = msg + "domain %s server %s.%s" % (p['NISDOMAIN'],p['server'],p['NISDOMAIN'])
		fp = open('/etc/yp.conf','w')
		fp.write(msg)
		fp.close()

	def updateNsswitch(self):
		msg = """
passwd:     files nis
shadow:     files nis
group:      files nis

hosts:      files dns nis

bootparams: nisplus [NOTFOUND=return] files

ethers:     files
netmasks:   files
networks:   files
protocols:  files
rpc:        files
services:   files

netgroup:   nisplus

publickey:  nisplus

automount:  files nisplus
aliases:    files nisplus
"""
		fp = open('/etc/nsswitch.conf','w')
		fp.write(msg)
		fp.close()
		
		
	def updateDB(self, params):
		"""
		make -C /var/yp
		"""
		cmd_str = ["make","-C","/var/yp"]
		return self.run(cmd_str)
